//
//  DNBaseNavigationController.swift
//  LunalerApp
//
//  Created by 露乐ios on 2018/2/6.
//  Copyright © 2018年 露乐ios. All rights reserved.
//

import UIKit

class DNBaseNavigationController: UINavigationController ,UIGestureRecognizerDelegate,UINavigationControllerDelegate {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        let appearance = UIBarButtonItem.appearance()
        appearance.setBackButtonTitlePositionAdjustment(UIOffset.init(horizontal: 0.0, vertical: -60), for: .default)
        self.navigationBar.isTranslucent = false
        // 2F88FF
        self.navigationBar.barTintColor = UIColor.init(red: 0x2f/255.0, green: 0x88/255.0, blue: 0xff/255.0, alpha: 1.0)
        
        let navBar = UINavigationBar.appearance()
        let image = dnCreatImageWithColor(color: kAppThemeColor)
        navBar.setBackgroundImage(image, for: UIBarPosition.any, barMetrics: .default)
        
        let shadowImage = dnCreatImageWithColor(color: kAppThemeColor)
        navBar.shadowImage = shadowImage
        
        
        #if swift(>=4.0)
            self.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 18.0, weight: .medium)]
        #elseif swift(>=3.0)
            self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white, NSFontAttributeName: UIFont.systemFont(ofSize: 18.0, weight: .medium)];
        #endif
        self.navigationBar.tintColor = UIColor.white
        
        self.interactivePopGestureRecognizer?.delegate = self
        self.delegate = self;
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            let image = #imageLiteral(resourceName: "btn_next_white.png").withRenderingMode(.alwaysOriginal)
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.init(image: image, style: .plain, target: self, action: Selector(("backPopDidOncliked:")));
            
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    func backPopDidOncliked(barItem:UIBarButtonItem){
        self.popViewController(animated: true)
    }
    
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        if self.responds(to: #selector(getter: UINavigationController.interactivePopGestureRecognizer)) {
            self.interactivePopGestureRecognizer?.isEnabled = true
        }
    }
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return self.childViewControllers.count>1
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
